
# 0) Prepare data ####

#please download the Latinobarometro Data from 2020. It is available here: https://www.latinobarometro.org/latContents.jsp (as of 17.11.2023)

if(!file.exists("./clean_data/lb.rds")){

  lb <- readRDS("./nonesharable_data/Latinobarometro_2020_Eng_Rds_v1_0.rds") %>%
  dplyr::select(country = idenpa,
                trade_US = p32na,
                trade_CN = p32n_b,
                good_CN = p33n,
                bad_CN = p34n,
                sympathy_US = p30st_a,
                sympathy_CN = p30st_c,
                relation_US = p31st_a,
                relation_CN = p31st_c,
                trade_support = p29st_b,
                left_right = p18st,
                sat_demo = P11STGBS_A,
                sat_econ = P11STGBS_B,
                age = s22_c,
                age_edu = s15,
                gender = sexo,
                occ = s25,
                occ_alt = s24_b) %>%
  mutate(country = factor(country,
                          levels = c("32", "68", "76",
                                     "152", "170", "188",
                                     "214", "218", "222",
                                     "320", "340", "484",
                                     "558", "591", "600",
                                     "604", "724", "858",
                                     "862"),
                          labels = c("ARG", "BOL", "BRA",
                                     "CHL", "COL", "CRI",
                                     "DOM", "ECU", "SLV",
                                     "GTM", "HND", "MEX",
                                     "NIC", "PAN", "PRY",
                                     "PER", "ESP", "URY",
                                     "VEN")),
         trade_US = factor(trade_US,
                           levels = c(4:1),
                           labels = c("Very unfavourable", "Somewhat unfavourable",
                                      "Somewhat favourable", "Very favourable")),
         trade_CN = factor(trade_CN,
                           levels = c(4:1),
                           labels = c("Very unfavourable", "Somewhat unfavourable",
                                      "Somewhat favourable", "Very favourable")),
         good_CN = factor(good_CN,
                          levels = c(1:9),
                          labels = c("Cheaper products", "Good-quality products", 
                                     "Access to more products", "Rapid improvement in tech products", 
                                     "A large market for us to sell to", "Investment in infrastructure projects", 
                                     "Other", "All", "None")),
         bad_CN = factor(bad_CN,
                         levels = c(1:9),
                         labels = c("Low-quality products", "Human rights problems in their country",
                                    "Low safety standards for products and food", "Low environmental standards",
                                    "Their workers take our jobs because they work for less",
                                    "Increase in debt to Chinese government or business interest", 
                                    "Other", "All", "None")),
         sympathy_US = factor(sympathy_US,
                              levels = c(4:1),
                              labels = c("Very unfavourable", "Somewhat unfavourable",
                                         "Somewhat favourable", "Very favourable")),
         sympathy_CN = factor(sympathy_CN,
                              levels = c(4:1),
                              labels = c("Very unfavourable", "Somewhat unfavourable",
                                         "Somewhat favourable", "Very favourable")),
         relation_US = factor(relation_US,
                              levels = c(4:1),
                              labels = c("Very poor", "Fairly poor",
                                         "Fairly good", "Very good")),
         relation_CN = factor(relation_CN,
                              levels = c(4:1),
                              labels = c("Very poor", "Fairly poor",
                                         "Fairly good", "Very good")),
         trade_support = factor(trade_support,
                                levels = c(4:1),
                                labels = c("Strongly disagree", "Disagree",
                                           "Agree", "Strongly agree")),
         left_right = ifelse(left_right %in% c(0:10), left_right, NA),
         sat_demo = factor(sat_demo,
                           levels = c(4:1),
                           labels = c("Not satisfied at all", "Not very satisfied",
                                      "Quite satisfied", "Very satisfied")),
         sat_econ = factor(sat_econ,
                           levels = c(4:1),
                           labels = c("Not satisfied at all", "Not very satisfied",
                                      "Quite satisfied", "Very satisfied")),
         age = ifelse(age == -2, NA, 2020-age),
         age_edu = ifelse(age_edu < 0, NA, 
                          ifelse(age_edu > 30, 30, age_edu)),
         gender = factor(gender,
                         levels = c(1,2),
                         labels = c("male","female")),
         occ = factor(ifelse(occ %in% c(1,2,5:7) | occ_alt %in% c(1,2,5:7), "High",
                             ifelse(occ  %in% c(3,4,8) | occ_alt  %in% c(3,4,8), "Low", NA)),
                      levels = c("Low", "High"))) %>%
  group_by(country) %>%
  mutate(id = paste0(country,"_", 1:n()),
         trade_diff = as.numeric(trade_US) - as.numeric(trade_CN)) %>% 
  dplyr::left_join(., readRDS("./nonesharable_data/unga_clean.RDS") %>% 
                     dplyr::select(country = country_short2, year, dist_avg) %>% 
                     dplyr::filter(year == 2019) %>% 
                     mutate(country = ifelse(country == "URU", "URY",
                                             ifelse(country == "PAR", "PRY",
                                                    ifelse(country == "HON", "HND",
                                                           ifelse(country == "GUA", "GTM",
                                                                  ifelse(country == "COS", "CRI", country)))))),
                   by = c("country")) %>% 
  dplyr::left_join(., readRDS("./nonesharable_data/trade_clean.RDS") %>% 
                     dplyr::filter(flow == "X", !is.na(agreement), agreement != "WLD", year == 2019) %>% 
                     pivot_wider(., id_cols = c(year, flow, country), names_from = agreement, values_from = total_trade) %>% 
                     mutate(shareuscn = log(USA / CHN)) %>% 
                     ungroup() %>% 
                     dplyr::select(country, contains("share")),
                   by = c("country")) %>% 
  dplyr::rename(country_short = country)

saveRDS(lb, "clean_data/lb.rds")

lb_reg <- dplyr::left_join(lb, lb %>%
                             ungroup() %>%
                             dplyr::select(id, contains("trade_"), contains("sympathy_"), contains("relation_"), contains("sat_"), dist_avg) %>%
                             mutate_at(vars(-id), as.numeric) %>%
                             rename_at(vars(-id), function(x) paste0(x,"_num")),
                           by = "id") %>%
  mutate(trade_diff = trade_US_num - trade_CN_num,
         trade_diff2 = factor(ifelse(trade_diff > 0, "Pro-USA",
                                     ifelse(trade_diff < 0, "Pro-CN",
                                            ifelse(trade_diff == 0, "Neutral", NA))),
                              levels = c("Neutral", "Pro-USA", "Pro-CN")),
         sympathy_diff = sympathy_US_num - sympathy_CN_num,
         relation_diff = relation_US_num - relation_CN_num,
         good_CN = factor(good_CN,
                          levels = levels(good_CN),
                          labels = c("Cheaper products", "Quality products",
                                     "More products", "Better tech products",
                                     "Market access in CN", "Infra. investment",
                                     "Other", "All", "None")),
         bad_CN = factor(bad_CN,
                         levels = levels(bad_CN),
                         labels = c("Low quality products", "Human rights",
                                    "Low safety stand.", "Low env. stand.",
                                    "CN workers take our jobs", "Increased debt to CN",
                                    "Other", "All", "None")),
         age_edu = factor(ifelse(age_edu > 18, "High",
                                 ifelse(age_edu < 19, "Low", NA)),
                          levels = c("Low", "High")))

saveRDS(lb_reg, "clean_data/lb_reg.rds")
rm(list = ls())
}

lb <- readRDS("clean_data/lb.rds")
lb_reg <- readRDS("clean_data/lb_reg.rds")

# 1) Figure 1 ####

rbind(lb %>% 
        select(country_short, trade_diff),
      lb %>% 
        select(country_short, trade_diff) %>% 
        mutate(country_short = "OVR")) %>% 
  mutate(country_short = factor(country_short,
                                levels = c("OVR", "ARG", "BOL", "BRA",
                                           "CHL", "COL", "CRI", "DOM",
                                           "ECU", "GTM", "HND", "MEX",
                                           "NIC", "PAN", "PER", "PRY",
                                           "SLV", "URY", "VEN"))) %>% 
  drop_na() %>%
  group_by(country_short) %>%
  summarise(mean = mean(trade_diff),
            se = plotrix::std.error(trade_diff)) %>%
  ggplot(., aes(x = forcats::fct_rev(country_short),
                y = mean,
                ymin = mean - 1.96*se,
                ymax = mean + 1.96*se)) + 
  geom_pointrange() +
  geom_hline(yintercept = 0, lty = "dotted") +
  coord_flip() + 
  theme_minimal() +
  labs(x = "Country", y = "Mean Preferences for USA-China") +
  NULL

ggsave("figures/Figure1.png", dpi = 600,
       width = 20, 
       height = 12, unit = "cm")

# 2) Descriptive information used in the descriptions

rbind(lb_reg %>% 
        select(country_short, trade_diff2),
      lb_reg %>% 
        select(country_short, trade_diff2) %>% 
        mutate(country_short = "OVR")) %>% 
  mutate(country_short = factor(country_short,
                                levels = c("OVR", "ARG", "BOL", "BRA",
                                           "CHL", "COL", "CRI", "DOM",
                                           "ECU", "GTM", "HND", "MEX",
                                           "NIC", "PAN", "PER", "PRY",
                                           "SLV", "URY", "VEN"))) %>% 
  drop_na() %>%
  group_by(country_short) %>%
  summarise(mean = mean(as.numeric(trade_diff2 == "Pro-USA")),
            se = plotrix::std.error(as.numeric(trade_diff2 == "Pro-USA"))) 

rbind(lb_reg %>% 
        select(country_short, trade_diff2),
      lb_reg %>% 
        select(country_short, trade_diff2) %>% 
        mutate(country_short = "OVR")) %>% 
  mutate(country_short = factor(country_short,
                                levels = c("OVR", "ARG", "BOL", "BRA",
                                           "CHL", "COL", "CRI", "DOM",
                                           "ECU", "GTM", "HND", "MEX",
                                           "NIC", "PAN", "PER", "PRY",
                                           "SLV", "URY", "VEN"))) %>% 
  drop_na() %>%
  group_by(country_short) %>%
  summarise(mean = mean(as.numeric(trade_diff2 == "Pro-CN")),
            se = plotrix::std.error(as.numeric(trade_diff2 == "Pro-CN"))) 


# 3) Table 2 ####

m1 <- lme4::lmer(trade_diff ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                           lb_reg)
summary(m1)

m2 <- nnet::multinom(trade_diff2 ~ left_right + relation_diff + dist_avg +age_edu + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + country_short,
                                lb_reg)
summary(m2)

texreg::screenreg(list(m1, m2),
                  beside = T)

texreg::htmlreg(list(m1, m2), 
                beside = T, 
                omit.coef = "country",
                file = "./tables/table2.html", 
                single.row = F, 
                leading.zero = T, 
                custom.coef.map = list("(Intercept)" = "Intercept",
                                       "age_eduHigh" = "Education high",
                                       "relation_diff" = "Perception relationship",
                                       "left_right" = "Political ideology",
                                       "age" = "Age",
                                       "genderfemale" = "Gender (female)",
                                       "occHigh" = "High-skilled employment",
                                       "trade_support_num" = "Trade support",
                                       "sat_demo_num" = "Satisfaction with democracy",
                                       "sat_econ_num" = "Satisfaction with the economy",
                                       "dist_avg" = "Disagreement in UNGA voting",
                                       "shareuscn" = "Relative trade ties"),
                reorder.coef = c(2, 7, 3, 11, 4, 5:6, 8:10, 12, 1))

# 4) Descriptive information for the text####
cor.test(as.numeric(lb_reg$age_edu), as.numeric(lb_reg$occ), method = "spearman")

# 5) Figure 2 ####

table(lb_reg$good_CN)

lb_reg$pro_cheap <- factor(ifelse(lb_reg$good_CN %in% c("Cheaper products", "All"), 1, 0))
lb_reg$pro_quality <- factor(ifelse(lb_reg$good_CN %in% c("Quality products", "All"), 1, 0))
lb_reg$pro_more <- factor(ifelse(lb_reg$good_CN %in% c("More products", "All"), 1, 0))
lb_reg$pro_tech <- factor(ifelse(lb_reg$good_CN %in% c("Better tech products", "All"), 1, 0))
lb_reg$pro_market <- factor(ifelse(lb_reg$good_CN %in% c("Market access in CN", "All"), 1, 0))
lb_reg$pro_infra <- factor(ifelse(lb_reg$good_CN %in% c("Infra. investment", "All"), 1, 0))

m_pro_cheap <- lme4::glmer(pro_cheap ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                           lb_reg, family=binomial(link = "logit"))
m_pro_quality <- lme4::glmer(pro_quality ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                             lb_reg, family=binomial(link = "logit"))
m_pro_more <- lme4::glmer(pro_more ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                          lb_reg, family=binomial(link = "logit"))
m_pro_tech <- lme4::glmer(pro_tech ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                          lb_reg, family=binomial(link = "logit"))
m_pro_market <- lme4::glmer(pro_market ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                            lb_reg, family=binomial(link = "logit"))
m_pro_infra <- lme4::glmer(pro_infra ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                           lb_reg, family=binomial(link = "logit"))

data.frame(mod = rep(c(levels(lb_reg$good_CN)[1:6]), each = 12),
           var = c(names(lme4::fixef(m_pro_cheap)),
                   names(lme4::fixef(m_pro_quality)),
                   names(lme4::fixef(m_pro_more)),
                   names(lme4::fixef(m_pro_tech)),
                   names(lme4::fixef(m_pro_market)),
                   names(lme4::fixef(m_pro_infra))),
           coef = c(lme4::fixef(m_pro_cheap),
                    lme4::fixef(m_pro_quality),
                    lme4::fixef(m_pro_more),
                    lme4::fixef(m_pro_tech),
                    lme4::fixef(m_pro_market),
                    lme4::fixef(m_pro_infra)),
           se = c(arm::se.fixef(m_pro_cheap),
                  arm::se.fixef(m_pro_quality),
                  arm::se.fixef(m_pro_more),
                  arm::se.fixef(m_pro_tech),
                  arm::se.fixef(m_pro_market),
                  arm::se.fixef(m_pro_infra))) %>% 
  mutate(var = factor(var,
                      levels = c("(Intercept)", "age_eduHigh", "occHigh",
                                 "relation_diff", "dist_avg",
                                 "left_right",
                                 "age", "genderfemale", "trade_support_num",
                                 "sat_demo_num", "sat_econ_num",  "shareuscn"),
                      labels = c("Intercept", "Education high", "High-skilled employment",
                                 "Perception relationship", "US Disagreement in UNGA voting",
                                 "Political ideology",
                                 "Age", "Gender (female)", "Trade support", 
                                 "Satisfaction with democracy", "Satisfaction with the economy", "Relative trade ties")),
         lower = coef - 1.96*se,
         upper = coef + 1.96*se,
         dim = factor(ifelse(var %in% c("Education high", "High-skilled employment"),
                             "Economic",
                             ifelse(var %in% c("Perception relationship", "US Disagreement in UNGA voting"),
                                    "Geopolitics", 
                                    ifelse(var %in% c("Political ideology", "Relative trade ties"),
                                           "Ideology", NA))),
                      levels = c("Economic", "Geopolitics", "Ideology"))) %>%
  dplyr::filter(var %in% c("Education high", "High-skilled employment",
                    "Perception relationship", "US Disagreement in UNGA voting",
                    "Political ideology", "Relative trade ties")) %>% 
  ggplot(., aes(x = forcats::fct_rev(mod), y = coef, ymin = lower, ymax = upper)) +
  geom_hline(yintercept = 0, lty = "dashed") +
  geom_pointrange() +
  coord_flip() +
  ggh4x::facet_nested_wrap(~dim+var, nrow = 3, scales = "free_x") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  labs(x = "Chinese products are good because:\n", y = "\nCoefficient") +
  NULL

ggsave("figures/Figure2.png", dpi = 600,
       width = 20, 
       height = 14, unit = "cm")

# 6) Figure 3 ####

table(lb_reg$bad_CN)

lb_reg$con_quality <- factor(ifelse(lb_reg$bad_CN %in% c("Low quality products", "All"), 1, 0))
lb_reg$con_human <- factor(ifelse(lb_reg$bad_CN %in% c("Human rights", "All"), 1, 0))
lb_reg$con_safety <- factor(ifelse(lb_reg$bad_CN %in% c("Low safety stand.", "All"), 1, 0))
lb_reg$con_env <- factor(ifelse(lb_reg$bad_CN %in% c("Low env. stand.", "All"), 1, 0))
lb_reg$con_jobs <- factor(ifelse(lb_reg$bad_CN %in% c("CN workers take our jobs", "All"), 1, 0))
lb_reg$con_debt <- factor(ifelse(lb_reg$bad_CN %in% c("Increased debt to CN", "All"), 1, 0))

m_con_quality <- lme4::glmer(con_quality ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                             lb_reg, family=binomial(link = "logit"))
m_con_human <- lme4::glmer(con_human ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                           lb_reg, family=binomial(link = "logit"))
m_con_safety <- lme4::glmer(con_safety ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                            lb_reg, family=binomial(link = "logit"))
m_con_env <- lme4::glmer(con_env ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                         lb_reg, family=binomial(link = "logit"))
m_con_jobs <- lme4::glmer(con_jobs ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                          lb_reg, family=binomial(link = "logit"))
m_con_debt  <- lme4::glmer(con_debt  ~ age_edu + relation_diff + left_right + dist_avg + occ + shareuscn + trade_support_num +sat_demo_num + sat_econ_num + age  + gender + (1 | country_short),
                           lb_reg, family=binomial(link = "logit"))

texreg::screenreg(list(m_con_quality, m_con_human, m_con_safety, m_con_env, m_con_jobs, m_con_debt))

data.frame(mod = rep(c(levels(lb_reg$bad_CN)[1:6]), each = 12),
           var = c(names(lme4::fixef(m_con_quality)),
                   names(lme4::fixef(m_con_human)),
                   names(lme4::fixef(m_con_safety)),
                   names(lme4::fixef(m_con_env)),
                   names(lme4::fixef(m_con_jobs)),
                   names(lme4::fixef(m_con_debt))),
           coef = c(lme4::fixef(m_con_quality),
                    lme4::fixef(m_con_human),
                    lme4::fixef(m_con_safety),
                    lme4::fixef(m_con_env),
                    lme4::fixef(m_con_jobs),
                    lme4::fixef(m_con_debt)),
           se = c(arm::se.fixef(m_con_quality),
                  arm::se.fixef(m_con_human),
                  arm::se.fixef(m_con_safety),
                  arm::se.fixef(m_con_env),
                  arm::se.fixef(m_con_jobs),
                  arm::se.fixef(m_con_debt))) %>% 
  mutate(var = factor(var,
                      levels = c("(Intercept)", "age_eduHigh", "occHigh",
                                 "relation_diff", "dist_avg",
                                 "left_right",
                                 "age", "genderfemale", "trade_support_num",
                                 "sat_demo_num", "sat_econ_num",  "shareuscn"),
                      labels = c("Intercept", "Education high", "High-skilled employment",
                                 "Perception relationship", "US Disagreement in UNGA voting",
                                 "Political ideology",
                                 "Age", "Gender (female)", "Trade support", 
                                 "Satisfaction with democracy", "Satisfaction with the economy", "Relative trade ties")),
         lower = coef - 1.96*se,
         upper = coef + 1.96*se,
         dim = factor(ifelse(var %in% c("Education high", "High-skilled employment"),
                             "Economic",
                             ifelse(var %in% c("Perception relationship", "US Disagreement in UNGA voting"),
                                    "Geopolitics", 
                                    ifelse(var %in% c("Political ideology", "Relative trade ties"),
                                           "Ideology", NA))),
                      levels = c("Economic", "Geopolitics", "Ideology"))) %>%
  dplyr::filter(var %in% c("Education high", "High-skilled employment",
                    "Perception relationship", "US Disagreement in UNGA voting",
                    "Political ideology", "Relative trade ties")) %>% 
  ggplot(., aes(x = forcats::fct_rev(mod), y = coef, ymin = lower, ymax = upper)) +
  geom_hline(yintercept = 0, lty = "dashed") +
  geom_pointrange() +
  coord_flip() +
  ggh4x::facet_nested_wrap(~dim+var, nrow = 3, scales = "free_x") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  labs(x = "Chinese products are bad because:", y = "Coefficient") +
  NULL

ggsave("figures/Figure3.png", dpi = 600,
       width = 20, 
       height = 14, unit = "cm")

